#!/bin/sh
\. ../../../nvm.sh

set -e

if [ -f .nvmrc ]; then mv .nvmrc .nvmrc.orig; fi

TEST_VERSION="v0.2.4"

cleanup () {
  nvm cache clear
  nvm deactivate
  nvm unalias default
  rm -rf ${NVM_DIR}/v* .nvmrc
  if [ -f .nvmrc.orig ]; then mv .nvmrc.orig .nvmrc; fi
  unset -f nvm_ls_remote nvm_ls_remote_iojs
}

die () {
  echo "$@"
  cleanup
  exit 1
}

nvm deactivate 2>/dev/null || die 'unable to deactivate'

\. ../../common.sh

REMOTE="$PWD/mocks/nvm_ls_remote.txt"
nvm_ls_remote() {
  cat "$REMOTE"
}
REMOTE_IOJS="$PWD/mocks/nvm_ls_remote_iojs.txt"
nvm_ls_remote_iojs() {
  cat "$REMOTE_IOJS"
}

make_fake_node "$TEST_VERSION"

# 1. install

nvm install -w "$TEST_VERSION" || die "\`nvm install -w $TEST_VERSION\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "$(cat .nvmrc)" \
  || die "\`nvm install -w $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

#

unset OUTPUT

# 2. use

nvm use -w "$TEST_VERSION" || die "\`nvm use -w $TEST_VERSION\` failed"
OUTPUT="$(cat .nvmrc)"

nvm_is_valid_version "$(cat .nvmrc)" \
  || die "\`nvm use -w $TEST_VERSION\`+ \`cat .nvmrc\` outputted invalid version: got '${OUTPUT}'"

#

cleanup
